package leetcode.BinaryTree;

import java.util.ArrayList;
import java.util.List;

public class PreorderTraversal {

    /**
     * 二叉树前序遍历
     * @param root
     * @return
     */
    public static List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> list = new ArrayList<Integer>();
        getIntegers(root, list);
        return list;
    }

    private static void getIntegers(TreeNode root, List<Integer> list) {
        if (root != null) {
            list.add(root.val);
            if (root.left != null) {
                getIntegers(root.left,list);
            }
            if (root.right != null) {
                getIntegers(root.right,list);
            }
        }
    }

    public static void main(String[] args) {
        TreeNode node1  = new TreeNode(1);
        TreeNode node2  = new TreeNode(2);
        TreeNode node3  = new TreeNode(3);

        node1.right = node2;
        node2.left = node3;

        preorderTraversal(node1);

    }
}

